//
//  TabBarController.m
//  自定义TabBar
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 zhu. All rights reserved.
//

#import "TabBarController.h"
#import "BaseButton.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"
#import "LZPlayViewController.h"
#import "LZMusicSingleton.h"
#import "ZBBRadioViewController.h"
static TabBarController *tabBar;
@interface TabBarController ()
@property(nonatomic, strong)ZBBButton *selectedBtn;
@property(nonatomic, assign)NSInteger count;
@property(nonatomic, strong)NSTimer *imageTimer;
@property(nonatomic, strong)NSMutableArray *LZarray;
@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [NSMutableArray array];
    CGRect rect = self.tabBar.frame;
    [self.tabBar removeFromSuperview];//移除tabbarcontroller自带的下部的条
    _myView  = [[UIView alloc]init];
    _myView.frame = rect;
    NSArray *array = @[@"发现", @"定制听", @"搜索", @"我的"];
    [self.view addSubview:_myView];
    for (NSInteger i = 0; i < 4; i++) {
        CGFloat x = i * _myView.frame.size.width / 5 + 5 * FITWidth;
         if (i >= 2) {
              CGFloat x = ((i + 1) * _myView.frame.size.width / 5 );
             ZBBButton *btn = [[ZBBButton alloc]initWithFrame:CGRectMake(x, 0, _myView.frame.size.width / 5, _myView.frame.size.height)];
             [_myView addSubview:btn];
             btn.tag = 1000 + i;
             [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
             NSString *imageName = [NSString stringWithFormat:@"TabBar%ld@2x", i + 1];
             NSString *imageNameSel = [NSString stringWithFormat:@"TabBar%ldSel@2x", i + 1];
             [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
             [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];
             btn.titleLabel.font = [UIFont systemFontOfSize:11];
             [btn setTitle:array[i] forState:UIControlStateNormal];
             [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
             [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
             [_array addObject:btn];
             

        }
       else
       {
           ZBBButton *btn = [[ZBBButton alloc]initWithFrame:CGRectMake(x, 0, _myView.frame.size.width / 5, _myView.frame.size.height)];
           [_myView addSubview:btn];
           btn.tag = 1000 + i;
           [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
           NSString *imageName = [NSString stringWithFormat:@"TabBar%ld@2x", i + 1];
           NSString *imageNameSel = [NSString stringWithFormat:@"TabBar%ldSel@2x", i + 1];
           [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
           [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];
           btn.titleLabel.font = [UIFont systemFontOfSize:11];
           [btn setTitle:array[i] forState:UIControlStateNormal];
           [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
           [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
           [_array addObject:btn];
           
           if (i == 0) {
               btn.selected = YES;
               self.selectedBtn = btn;
           }

      }
        
    
    }
    [self.view addSubview:_myView];
    
    self.zzzView.backgroundColor = [UIColor whiteColor];
    self.zzzView.layer.cornerRadius = 27 * FITHeight;
    self.imageView.layer.cornerRadius = 27 * FITHeight;
    self.imageView.layer.masksToBounds  = YES;
    self.imageView.backgroundColor = [UIColor redColor];
    
    self.playButton.backgroundColor = [UIColor  whiteColor];
    self.playButton.layer.cornerRadius = 27 * FITHeight;
    self.playButton.alpha = 0.0;
   [self.playButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
   

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewAction)];
    [self.imageView addGestureRecognizer:tap];
   
    
    
   }
-(UIView *)zzzView
{
    if (_zzzView == nil) {
        _zzzView = [[UIView alloc]initWithFrame:CGRectMake(155 * FITWidth, -15 * FITHeight, 60 * FITWidth, 90 * FITHeight)];
        [_myView addSubview:_zzzView];
//        _zzzView.layer.borderWidth = 1.0;
    }
    return _zzzView;
}
-(ZBBButton *)playButton
{
    if (_playButton == nil) {
        _playButton = [[ZBBButton alloc]initWithFrame:CGRectMake(3 * FITWidth, 5 * FITHeight, 54 * FITWidth, 54 * FITHeight)];
        [_zzzView addSubview:_playButton];
        [_playButton setImage:[UIImage imageNamed:@"zbbpause@3x.png"] forState:UIControlStateNormal];
        _playButton.flag = 1;
        
    }
    return _playButton;
    
}

-(void)clickBtn:(ZBBButton *)button
{
        self.selectedBtn.selected = NO;
        button.selected = YES;
        self.selectedBtn = button;
     self.selectedIndex = button.tag - 1000;
   
}


-(void)buttonAction
{
  if ([[NSUserDefaults standardUserDefaults] integerForKey:@"play"]) {
             [LZPlayViewController shareInstance].playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
             [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
             [[LZMusicSingleton shareInstance].player play];
             _playButton.alpha = 0;
         }
    [self presentViewController:[LZPlayViewController shareInstance] animated:YES completion:nil];

}
-(void)AudioWillPlay:(BOOL)isTransform
{
    if (!isTransform) {
            _count++;
        if (_count == 1) {
          [_imageTimer invalidate];
            _imageTimer = nil;
            _count = 0;
        }
        _imageTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(imageViewTransform) userInfo:nil repeats:YES];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addTimer:_imageTimer forMode:NSRunLoopCommonModes];
        

    }
    else
    {
         [_imageTimer invalidate];
          _imageTimer = nil;
    
           }
}


-(void)imageViewTransform
{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 2 * M_PI / 60.0 / 10);
}
-(void)imageViewAction
{
    
    if (!_flagInit) {
        [self presentViewController:[LZPlayViewController shareInstance] animated:YES completion:nil];
    }
    else
    {
      
     [_nav pushViewController:[ZBBRadioViewController shareInstance] animated:YES];
    }
   }
-(UIImageView *)imageView
{
    
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(3 * FITWidth, 5 * FITHeight, 54 * FITWidth, 54 * FITHeight)];
        _imageView.userInteractionEnabled = YES;
        
        [_zzzView addSubview:_imageView];
    }
    return _imageView;
    
}

//只留一个播放按钮
-(void)remainButtonOfPlay:(BOOL)hiden
{
     if (hiden) {
    for (ZBBButton *button in _array) {
            button.hidden = YES;
        
        }
         _myView.backgroundColor = [UIColor clearColor];
    }
    else
    {
        for (ZBBButton *button in _array) {
            button.hidden = NO;
        }
        _myView.backgroundColor = [UIColor whiteColor];
    }
}
//全部隐藏
-(void)totalTabBarShouldHiden:(BOOL)hiden
{
    if (hiden) {
        _myView.hidden = YES;
    }else{
        _myView.hidden = NO;
    }
}
+(TabBarController *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
      
        tabBar = [[TabBarController alloc]init];
        
});
    
    return tabBar;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
